﻿using System;
using System.Collections.Generic;
using System.Linq;
using TuxedoLib;

namespace SpaceSquirrel
{
	public class GameOverScene : GameSceneBase
	{
		private Context context;
		private int counter = 0;
		private int index = 0;
		private string mode = "prompt";
		private string password;

		public GameOverScene(Context context)
			: base()
		{
			this.context = context;
			this.password = context.ConvertToPassword();
			context.PowerUpsTaken.Clear();
		}

		public override void ProcessInput(List<MyEvent> events, Dictionary<string, bool> pressedActions)
		{
			if (this.mode == "prompt")
			{
				foreach (MyEvent ev in events)
				{
					if (ev.Down)
					{
						if (ev.Action == "up")
						{
							if (this.index == 1)
							{
								this.index = 0;
								SoundManager.PlayNoise("menu_move");
							}
						}
						else if (ev.Action == "down")
						{
							if (this.index == 0)
							{
								this.index = 1;
								SoundManager.PlayNoise("menu_move");
							}
						}
						else if (ev.Action == "A" || ev.Action == "B" || ev.Action == "start")
						{
							this.mode = "commit";
							this.counter = 0;
							SoundManager.PlayNoise("menu_select");
						}
					}
				}
			}
		}

		public override void Update()
		{
			JukeBox.EnsureSong("gameover");
			++this.counter;
			if (this.mode == "commit")
			{
				if (this.counter > 30)
				{
					if (this.index == 0)
					{
						this.Next = new PlayScene("ship_1", 8, 9, this.context);
					}
					else if (this.index == 1)
					{
						this.Next = new TitleScene();
					}
					else
					{
						// what?
						this.Next = new OpeningScene();
					}
				}
			}
		}

		public override void Render(Image screen, int renderCounter)
		{
			screen.Fill(0, 0, 0);
			bool showSelected = this.mode == "prompt" || (renderCounter % 2) == 0;
			Image gameOver = Text.Render(-1, "GAME OVER");
			Image optionA = Text.Render(-1, "CONTINUE");
			Image optionB = Text.Render(-1, "END");
			Image password = Text.Render(-1, "PASSWORD:");
			Image passwordValue = Text.Render(Draw.CreateColor(128, 128, 128), this.password);
			Image cursor = Text.Render(-1, ">");

			int left = 80;
			int top = 96;

			int x = left;
			int y = top;
			screen.Blit(gameOver, left, top);

			y += 16;
			if (this.index == 0)
			{
				screen.Blit(cursor, x, y);
			}
			if (showSelected || this.index == 1)
			{
				screen.Blit(optionA, x + 16, y);
			}

			y += 16;
			if (this.index == 1)
			{
				screen.Blit(cursor, x, y);
			}

			if (showSelected || this.index == 0)
			{
				screen.Blit(optionB, x + 16, y);
			}

			y += 48;
			x -= 32;
			screen.Blit(password, x, y);
			x += password.Width + 8;
			screen.Blit(passwordValue, x, y);
		}
	}
}
